﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace ImageTagToolViewModel.Others
{
    /// <summary>
    /// This class manages to save the settings.
    /// </summary>
    class SettingsProvider
    {
        /// <summary>
        /// A Settings type. It contains the stored items.
        /// </summary>
        private Settings s = null;

        /// <summary>
        /// The constructor. If the file does not exists, it is created with default values. Otherwise, load the stored values.
        /// </summary>
        public SettingsProvider()
        {
            String path = "settings.xml";
            XmlSerializer serializer = new XmlSerializer(typeof(Settings));

            if (!System.IO.File.Exists(path))
            {
                s = new Settings();
                s.AllowUpdate = false;
                s.AvailableFormats = "JPG, BMP";
                s.DefaultFolder = "C:\\";
                s.PrintMargins = "0, 0, 0, 0";
                WriteXML();
            }

            StreamReader reader = new StreamReader(path);
            this.s = (Settings)serializer.Deserialize(reader);
            reader.Close();
        }

        /// <summary>
        /// Default folder getter function.
        /// </summary>
        /// <returns>returns the default folder</returns>
        public String getDefaultFolder()
        {
            return this.s.DefaultFolder;
        }

        /// <summary>
        /// Available formats getter function.
        /// </summary>
        /// <returns>returns the available formats</returns>
        public String[] getAvailableFormats()
        {
            String formats = this.s.AvailableFormats;
            String[] separator = new String[] { ", " };
            String[] result = formats.Split(separator, StringSplitOptions.None);
            return result;
        }

        /// <summary>
        /// Print margins getter function.
        /// </summary>
        /// <returns>returns the print margins</returns>
        public int[] getPrintMargins()
        {
            String margins = this.s.PrintMargins;
            String[] separator = new String[] { ", " };
            String[] resultStr = margins.Split(separator, StringSplitOptions.None);
            int[] result = new int[4];
            for (int i = 0; i < 4; i++)
            {
                result[i] = int.Parse(resultStr[i]);
            }
            return result;
        }

        /// <summary>
        /// Update getter function
        /// </summary>
        /// <returns>a bool - update on?</returns>
        public bool getAllowUpdate()
        {
            return this.s.AllowUpdate;
        }

        /// <summary>
        /// Default folder setter function
        /// </summary>
        /// <param name="defaultFolder">the path</param>
        public void setDefaultFolder(String defaultFolder)
        {
            this.s.DefaultFolder = defaultFolder;
            WriteXML();
        }

        /// <summary>
        /// Available formats setter function
        /// </summary>
        /// <param name="formats">the available formats</param>
        public void setAvailableFormats(String[] formats)
        {
            String formatsStr = formats[0];
            for (int i = 1; i < formats.Length; i++)
            {
                formatsStr += ", " + formats[i];
            }
            this.s.AvailableFormats = formatsStr;
            WriteXML();
        }

        /// <summary>
        /// Print margins setter function
        /// </summary>
        /// <param name="margins">the margin values: left, top, right, bottom</param>
        public void setPrintMargins(int[] margins)
        {
            String marginsStr = margins[0].ToString();
            for (int i = 1; i < 4; i++)
            {
                marginsStr += ", " + margins[i].ToString();
            }
            this.s.PrintMargins = marginsStr;
            WriteXML();
        }

        /// <summary>
        /// update setter function
        /// </summary>
        /// <param name="update">bool - update on?</param>
        public void setAllowUpdate(bool update)
        {
            this.s.AllowUpdate = update;
            WriteXML();
        }

        /// <summary>
        /// This function writes the settings.xml file.
        /// </summary>
        private void WriteXML()
        {
            XmlSerializer writer = new XmlSerializer(typeof(Settings));
            System.IO.StreamWriter file = new System.IO.StreamWriter(@"settings.xml");
            writer.Serialize(file, this.s);
            file.Close();
        }



    }

    /// <summary>
    /// the serialized settings class.
    /// </summary>
    [Serializable()]
    public class Settings
    {
        [System.Xml.Serialization.XmlElement("DefaultFolder")]
        public String DefaultFolder { get; set; }
        [System.Xml.Serialization.XmlElement("AvailableFormats")]
        public String AvailableFormats { get; set; }
        [System.Xml.Serialization.XmlElement("PrintMargins")]
        public String PrintMargins { get; set; }
        [System.Xml.Serialization.XmlElement("AllowUpdate")]
        public bool AllowUpdate { get; set; }

    }
}